package pers.wq.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import pers.wq.util.StringUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseIndexAction extends ActionSupport{
	public static final String JSON = "json";
	public static final String ADD = "add";
	public static final String EDIT = "edit";
	public static final String EA = "ea";
	public static final String CHAIN = "chain";
	public static final String ID_TAG = "idtag";
	private MyArrayList action;
	private String loginTag;
	private String curAction;
	protected Map<String,String> jsonMap;
	private int id;
	
	public BaseIndexAction(String beanId) {
		// TODO Auto-generated constructor stub
		ApplicationContext factory = new ClassPathXmlApplicationContext("controllerMap.xml");
		action = (MyArrayList) factory.getBean(beanId);
		curAction = beanId;
		jsonMap = new HashMap();
	}
	
	public void setJsonMapError(String msg){
		jsonMap.put("code", "400");
		jsonMap.put("msg",msg);
	}
	public void setJsonMapSuccess(String msg){
		jsonMap.put("code", "200");
		jsonMap.put("msg", msg);
	}
	public String getLoginTag() {
		return loginTag;
	}
	protected void loginSession(int id) {
		ActionContext.getContext().getSession().put(getLoginTag(), SUCCESS);
		ActionContext.getContext().getSession().put(ID_TAG, id+"");
	}
	protected void loginSession() {
		ActionContext.getContext().getSession().put(getLoginTag(), SUCCESS);
	}
	public static String getSession(String key){
		return (String) ActionContext.getContext().getSession().get(key);
	}
	public static boolean containSession(String key) {
		return ActionContext.getContext().getSession().containsKey(key);
	}
	public String getActionName() {
		return this.getClass().getSimpleName();
	}

	public MyArrayList getAction() {
		return action;
	}

	public void setAction(MyArrayList action) {
		this.action = action;
	}
	public Map<String, String> getJsonMap() {
		return jsonMap;
	}
	public void setJsonMap(Map<String, String> jsonMap) {
		this.jsonMap = jsonMap;
	}
	public String map2Json(){
		return StringUtil.map2JsonStr(jsonMap);
	}
	public void setLoginTag(String loginTag) {
		this.loginTag = loginTag;
	}
	public String getCurAction() {
		return curAction;
	}
	public void setCurAction(String curAction) {
		this.curAction = curAction;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
}
